<template>
    <div class="admim-nav">
        <div>
            <el-icon>
                <Tickets />
            </el-icon>
            <span>
                数据列表
            </span>
        </div>
        <div>
            <el-button type="primary" @click="addmenu()">添加</el-button>
        </div>
    </div>
    <el-table :data="newData.value ? newData.value : tableData" border>
        <el-table-column prop="id" label="编号" width="100" height="40"></el-table-column>
        <el-table-column prop="name" label="分类名称" width="80" height="40"></el-table-column>
        <el-table-column label="级别" width="100" height="40">
            <template #default="scope">
                <span>一级</span>
            </template>
        </el-table-column>
        <el-table-column prop="productCount" label="商品数量" width="100" height="40"></el-table-column>
        <el-table-column prop="productUnit" label="数量单位" width="100" height="40"></el-table-column>
        <el-table-column prop="hidden" label="导航栏" width="100" height="40">
            <template #default="scope">
                <el-switch :value="scope.row.navStatus === 1 ? true : false" />
            </template>
        </el-table-column>
        <el-table-column prop="hidden" label="是否显示" width="100" height="40">
            <template #default="scope">
                <el-switch :value="scope.row.showStatus === 0 ? false : true" />
            </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" width="100" height="40"></el-table-column>
        <el-table-column label="设置" width="200" height="40">

            <template #default="scope">
                <el-button link type="primary" @click="viewSubordinates(scope.row)">查看下级</el-button>
                <el-button link type="primary">转移商品</el-button>
            </template>
        </el-table-column>
        <el-table-column label="操作" width="200" height="40">
            <!-- <el-button type="primary" @click.prevent="addmenus(scope.$index)">编辑</el-button> -->
            <template #default="scope">
                <el-button link type="primary" @click.prevent="addmenu(scope.row.id)">
                    编辑
                </el-button>
                <el-button link type="primary" @click="deleteFlashFn(scope.row.id)">
                    删除
                </el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination v-model:current-page="pagination.pageNum" v-model:page-size="pagination.pageSize"
        :page-sizes="[1, 5, 10, 20]" layout="total, sizes, prev, pager, next, jumper"
        :total="paginations ? paginations.total : pagination.total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
</template>

<script setup lang="ts">
import * as api from '../../request/api'
import useTable from '../../hooks/useTables'
import { ref } from "vue"
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter, useRoute, RouterView } from 'vue-router';
const router = useRouter()
const newData: any = ref([])
const paginations: any = ref()

let { tableData, getTableData, pagination } = useTable(api.product.getCommodityClassification)
console.log(tableData);


getTableData()
const handleSizeChange = (val: number) => {
    getTableData()
    // console.log(`${val} items per page`)
}
const handleCurrentChange = (val: number) => {
    getTableData()
    // console.log(`current page: ${val}`)
}
//添加
const addmenu = (id?: string) => {
    console.log(id);
    router.push({
        path: '/Pms/updateProductCate',
        query: { id }

    });

}

// 删除按钮
let deleteFlashFn = (id: number) => {
    // 二次 确认 
    ElMessageBox.confirm(
        '是否要删除该活动!', "提示",
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            // 发请求删除
            await api.product.postProductDeletion(id)
            ElMessage({
                type: 'success',
                message: '删除成功',
            })
            getTableData()
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消删除',
            })
        })
}

//查看下级
const viewSubordinates = (row: any) => {
    console.log(row);
    let { tableData, getTableData, pagination } = useTable(api.product.getproductSubordinate, {
        parentId: row.id,
    })
    getTableData()
    newData.value = tableData
    paginations.value = pagination
    console.log(newData);
    console.log(paginations);

    return newData
}
</script>

<style lang="scss" scoped>
.admim-nav {
    width: 96%;
    height: 20px;
    background-color: #fff;
    border: 1px solid #ccc;
    box-shadow: 0px 0px 3px 0px rgba(0, 0, 0, 0.2);
    padding: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 60px;
    margin-bottom: 20px;

    .example-pagination-block {
        margin-top: 10px;
        float: right;
    }
}
</style>